//
//  ChooseInstrumentViewController.m
//  Teacher
//
//  Created by 李飞 on 15/10/30.
//  Copyright (c) 2015年 xueyinyue. All rights reserved.
//

#import "ChooseInstrumentViewController.h"
#import "ChooseInstrumentViewCell.h"

#define ROW_HEIGHT                45

@interface ChooseInstrumentViewController ()

@end

@implementation ChooseInstrumentViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
     _chooseInstrumentNameArray = [[NSMutableArray alloc]init];
    _dataArray = @[@{@"小提琴":@7},@{@"中提琴":@8},@{@"大提琴":@9},@{@"低音提琴":@10},@{@"二胡":@11},@{@"板胡":@12},@{@"京胡":@13},@{@"单簧管":@14},@{@"双簧管":@15},@{@"长笛":@16},@{@"大管":@17},@{@"萨克斯":@18},@{@"小号":@19},@{@"圆号":@20},@{@"长号":@21},@{@"大号":@22},@{@"竹笛":@23},@{@"笙":@24},@{@"唢呐":@25},@{@"吉它":@26},@{@"贝斯":@27},@{@"尤克里里":@28},@{@"琵琶":@29},@{@"古筝":@30},@{@"阮":@31},@{@"柳琴":@32},@{@"扬琴":@33},@{@"竖琴":@34},@{@"架子鼓":@35},@{@"小军鼓":@36},@{@"马林巴":@37},@{@"中国大鼓":@38},@{@"排鼓":@39},@{@"钢琴":@40},@{@"电子琴":@41},@{@"双排键":@42},@{@"乐理":@43},@{@"视唱练耳":@44},@{@"声乐唱法":@45},@{@"作曲":@46},@{@"非洲鼓":@47},@{@"手风琴":@48},@{@"葫芦丝":@49},@{@"陶笛（箫）":@50}];
    
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


#pragma mark - UITableView

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _dataArray.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return ROW_HEIGHT;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIdentifier = @"ChooseInstrumentViewCell";
    ChooseInstrumentViewCell * cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier ];
    if (!cell) {
        cell = [[NSBundle mainBundle] loadNibNamed:@"ChooseInstrumentViewCell" owner:nil options:nil][0];
    }
    
    cell.instrumentNameLab.text = [_dataArray[indexPath.row] allKeys][0];
    
    for ( NSNumber* instrumentId  in _chooseArray )
    {
        if ([ [_dataArray[indexPath.row] allValues][0] isEqualToNumber: instrumentId])
        {
            cell.accessoryType = UITableViewCellAccessoryCheckmark;
            break;
        }        
    }
    
    return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [_instrumentTV deselectRowAtIndexPath:indexPath animated:YES];
    
    ChooseInstrumentViewCell * cell = (ChooseInstrumentViewCell *) [_instrumentTV  cellForRowAtIndexPath:indexPath];
    
    for (NSNumber * num in _chooseArray)
    {
        if ([ (NSNumber *) [_dataArray[indexPath.row] allValues][0] isEqualToNumber: num])
        {
            cell.accessoryType = UITableViewCellAccessoryNone;
            [_chooseArray removeObject:[_dataArray[indexPath.row] allValues][0]];
            return;
        }
    }
    
    cell.accessoryType = UITableViewCellAccessoryCheckmark;
    
    [_chooseArray addObject:[_dataArray[indexPath.row] allValues][0] ];
    
    NSLog(@"%@",_chooseArray);
    
}


- (IBAction)onOkBtnClick:(id)sender {
    _chooseString = [_chooseArray componentsJoinedByString:@","];
    
    for (NSNumber * num in _chooseArray)
    {
        for (NSDictionary * dc in _dataArray)
        {
            NSLog(@"num :%@",num);
            NSLog(@"[dc allValues][0] :%@",[dc allValues][0]);
            
            if ([ num isEqualToNumber: [dc allValues][0] ])
            {
                if(! _chooseInstrumentNameString)
                {
                    _chooseInstrumentNameString = [dc allKeys][0];
                    
                    NSLog(@"allKeys :%@",[dc allKeys][0]);
                    NSLog(@"_chooseInstrumentNameString111 :%@",_chooseInstrumentNameString);
                }
                else
                {
                   _chooseInstrumentNameString = [NSString stringWithFormat:@"%@,%@",_chooseInstrumentNameString,[dc allKeys][0]];
                    
                    NSLog(@"allKeys :%@",[dc allKeys][0]);
                    NSLog(@"_chooseInstrumentNameString111 :%@",_chooseInstrumentNameString);
                }
            }
        }
    }
    if (!_chooseInstrumentNameString) {
        _chooseInstrumentNameString = @"请选择";
    }
    NSLog(@"%@",_chooseString);
    NSLog(@"%@",_chooseInstrumentNameString);
    self.blockOfSetInstrument(_chooseString, _chooseInstrumentNameString);
        [self dismissViewControllerAnimated:YES completion:nil];
}
@end
